package com.spring.student.Dao;
import java.util.List;
import java.util.Map;

import com.spring.student.systemDB.Course;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
@Component("courseManage")
public class CourseManageImpl implements CourseManage{
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Override
    public void updateCourse(String courseName){
        String sql = "update courses set "+courseName+" = "+courseName+"-1";
        int num = jdbcTemplate.update(sql);
        if (num <=0)
            System.out.println("更新待选课程记录失败");
    }
    @Override
    public void queryCourse(){
        String sql = "select * from courses";
        RowMapper<Course> rowMapper;
        rowMapper = new BeanPropertyRowMapper<Course>(Course.class);
        Course course = jdbcTemplate.queryForObject(sql, rowMapper);
        System.out.println(course);

    }
    @Override
    @Transactional(propagation = Propagation.REQUIRED,readOnly = false,
            isolation = Isolation.DEFAULT)
    public void selectCourse(String number,String courseName){
        updateCourse(courseName);
        queryCourse();
        jdbcTemplate.execute("create table if not exists optional_course("
                +"number varchar(20),courseName varchar(20),score double)");
        String sql = "insert into optional_course(number,courseName,score) values(?,?,?)";
        jdbcTemplate.update(sql,number,courseName,0.0);
        System.out.println(("查询选课程信息记录"));
        sql = "select * from optional_courses where number = ?";
        List<Map<String,Object>> maps = jdbcTemplate.queryForList(sql,number);
        for (Map<String,Object> map:maps){
            System.out.println(map);
        }

    }
}
